﻿using UnityEngine;
using System.Collections;
using System;
using System.IO;
using UnityEngine.UI;
using EzFrameWork;
using System.Text;

public class CaptureScreen : MonoBehaviour
{
    RectTransform rect;
    private float width;
    private float height;
    private float offictX;
    private float offictY;
    public bool AutoScreenShot = false;
    private void Start()
    {
        rect = GetComponent<RectTransform>();
        width = rect.sizeDelta.x;
        height = rect.sizeDelta.y;
        offictX = rect.localPosition.x;
        offictY = rect.localPosition.y;
        if (AutoScreenShot)
        {
            CaptureScreenshot();
        }
    }

    /// <summary>
    /// 开始截图
    /// </summary>
    public void CaptureScreenshot()
    {
        StartCoroutine(SaveCapture());
    }

    /// <summary>
    /// 设置属性截图
    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="offictX"></param>
    /// <param name="offictY"></param>
    public void CaptureScreenshot(float width, float height, float offictX, float offictY)
    {
        StartCoroutine(SaveCapture(width, height, offictX, offictY));
    }

    /// <summary>
    /// 开启协成进行截图
    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="offictX"></param>
    /// <param name="offictY"></param>
    /// <returns></returns>
    IEnumerator SaveCapture(float width, float height, float offictX, float offictY)
    {
        string SaveCapturePath = Application.streamingAssetsPath + "/Pictures/ScreenShots";
        if (!Directory.Exists(SaveCapturePath))
        {
            Directory.CreateDirectory(SaveCapturePath);
        }

        string imageName = GetCurTime() + ".png";
        string name = SaveCapturePath + "/Screenshot_" + imageName;
        yield return new WaitForEndOfFrame();

        if (Screen.width <= width && Screen.height <= height)
        {
            Texture2D tex = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
            tex.ReadPixels(new Rect((Screen.width) * 0.5f, (Screen.height) * 0.5f, Screen.width, Screen.height), 0, 0);
            tex.Apply();
            byte[] bytes = tex.EncodeToPNG();
            Debug.Log("截图成功！！！");
            EzDataRWS.BytesToFile(Application.streamingAssetsPath + "Pictures/ScreenShot", name, bytes);
        }
        else
        {
            Texture2D tex = new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false);
            tex.ReadPixels(new Rect((Screen.width - width + offictX) * 0.5f, (Screen.height - height + offictY) * 0.5f, width, height), 0, 0);
            tex.Apply();
            byte[] bytes = tex.EncodeToPNG();
            Debug.Log("截图成功！！！");
            EzDataRWS.BytesToFile(Application.streamingAssetsPath + "Pictures/ScreenShot", name, bytes);
        }
    }
    IEnumerator SaveCapture()
    {
        string SaveCapturePath = Application.streamingAssetsPath + "/Pictures/ScreenShots";
        if (!Directory.Exists(SaveCapturePath))
        {
            Directory.CreateDirectory(SaveCapturePath);
        }

        string imageName = GetCurTime() + ".png";
        string name = "Screenshot_" + imageName;
        yield return new WaitForEndOfFrame();
        Texture2D tex = new Texture2D((int)width, (int)height, TextureFormat.ARGB32, false);
        tex.ReadPixels(new Rect((Screen.width - width + offictX) * 0.5f, (Screen.height - height + offictY) * 0.5f, width, height), 0, 0);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        Debug.Log("截图成功！！！");
        EzDataRWS.BytesToFile(Application.streamingAssetsPath + "/Pictures/ScreenShot", name, bytes);
    }

    //刷新图片，显示到相册中
    void ScanFile(string[] path)
    {
        using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
            using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
            {
                Conn.CallStatic("scanFile", playerActivity, path, null, null);
            }
        }
    }

    /// <summary>
    /// 获取当前年月日时分秒，如20181001444
    /// </summary>
    /// <returns></returns>
    string GetCurTime()
    {
        return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
            + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
    }
}